package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-hub/model/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateVideoLogic {
	return &UpdateVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateVideoLogic) UpdateVideo(in *videorpc.UpdateVideoRequest) (*videorpc.UpdateVideoResponse, error) {
	// todo: add your logic here and delete this line
	var video mysql.Video
	err := video.FindVideoId(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("视频不存在")
	}
	video = mysql.Video{
		Model:         gorm.Model{ID: video.ID},
		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
	}
	err = video.UpdateVideo(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &videorpc.UpdateVideoResponse{
		VideoId: int64(video.ID),
	}, nil
}
